package com.star.aries.utils.excel;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.star.aries.utils.DateUtils;

public class LocalDateConverter implements Converter<LocalDate> {

  /**
   * 不使用{@code @LocalDateTimeFormat}注解指定日期格式时,默认会使用该格式.
   */
  private static final String DEFAULT_PATTERN = "yyyy-MM-dd";

  @Override
  public Class<LocalDate> supportJavaTypeKey() {
    return LocalDate.class;
  }

  @Override
  public CellDataTypeEnum supportExcelTypeKey() {
    return CellDataTypeEnum.STRING;
  }

  /**
   * 这里读的时候会调用
   *
   * @param cellData            excel数据 (NotNull)
   * @param contentProperty     excel属性 (Nullable)
   * @param globalConfiguration 全局配置 (NotNull)
   * @return 读取到内存中的数据
   */
  @SuppressWarnings("rawtypes")
  @Override
  public LocalDate convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
      GlobalConfiguration globalConfiguration) {
    LocalDateTimeFormat annotation = contentProperty.getField().getAnnotation(LocalDateTimeFormat.class);
    return DateUtils.toLocalDate(cellData.getStringValue(), Objects.nonNull(annotation) ? annotation.value() : DEFAULT_PATTERN);
//    return LocalDate.parse(cellData.getStringValue(),
//        DateTimeFormatter
//            .ofPattern(Objects.nonNull(annotation) ? annotation.value() : DEFAULT_PATTERN));
  }


  /**
   * 写的时候会调用
   *
   * @param value               java value (NotNull)
   * @param contentProperty     excel属性 (Nullable)
   * @param globalConfiguration 全局配置 (NotNull)
   * @return 写出到excel文件的数据
   */
  @Override
  public CellData<String> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty,
      GlobalConfiguration globalConfiguration) {
    LocalDateTimeFormat annotation = contentProperty.getField()
        .getAnnotation(LocalDateTimeFormat.class);

    return new CellData<>(value.format(DateTimeFormatter
        .ofPattern(Objects.nonNull(annotation) ? annotation.value() : DEFAULT_PATTERN)));

  }
}
